package com.ins.common.exception;

import com.ins.common.enums.ErrEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;


/**
 * 自定义异常
 *
 * @author ins
 * @since 2023-3-10
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ServerException extends RuntimeException {

    @Serial
    private static final long serialVersionUID = -6451717002972712804L;

    private Object data;
    private int code = 500;
    private String msg;

    /**
     * 服务器异常
     *
     * @param msg 味精
     */
    public ServerException(String msg) {
        super(msg);
        this.msg = msg;
    }

    public ServerException(ErrEnum errEnum) {
        super(errEnum.getMsg());
        this.code = errEnum.getCode();
        this.msg = errEnum.getMsg();
    }

    public ServerException(ErrEnum errEnum, Object data) {
        super(errEnum.getMsg());
        this.code = errEnum.getCode();
        this.msg = errEnum.getMsg();
        this.data = data;
    }

    public ServerException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }

    /**
     * 服务异常
     *
     * @param msg 错误提示
     * @return {@link ServerException}
     */
    public static ServerException spe(String msg) {
        return new ServerException(msg);
    }
}